package jmine.tec.utils.version;

import java.io.Serializable;

/**
 * Representa uma versao do componente, tipicamente no formato MAJOR.MINOR.REVISION
 * 
 * @author takeshi
 */
public final class Version implements Comparable<Version>, Serializable {

    private final VersionComponent major, minor, revision;

    private final SCMRevision scmRevision;

    private final boolean snapshot;

    /**
     * C'tor
     * 
     * @param versionString String
     * @param scm {@link SCMRevision}
     */
    public Version(String versionString, SCMRevision scm) {
        super();
        String[] split = versionString.split("[.]");
        this.major = new VersionComponent(split[0]);
        this.minor = parseMinor(split);
        this.revision = parseRevision(split);
        this.snapshot = versionString.contains("SNAPSHOT");
        this.scmRevision = scm;
    }

    /**
     * @param versionString String
     */
    public Version(String versionString) {
        this(versionString, SCMRevision.NA);
    }

    /**
     * Faz o parse da minor version ou devolve {@link VersionComponent#NONE}
     * 
     * @param split array de String
     * @return {@link VersionComponent}
     */
    private static VersionComponent parseMinor(String[] split) {
        if (split.length > 1) {
            return new VersionComponent(split[1]);
        }
        return VersionComponent.NONE;
    }

    /**
     * @param split String[]
     * @return {@link VersionComponent}
     */
    private static VersionComponent parseRevision(String[] split) {
        if (split.length > 2) {
            return new VersionComponent(collect(split, 2));
        }
        return VersionComponent.NONE;
    }

    /**
     * @param split String
     * @param starting int
     * @return String
     */
    private static String collect(String[] split, int starting) {
        StringBuilder sb = new StringBuilder(split[starting]);
        for (int j = starting + 1; j < split.length; j++) {
            String string = split[j];
            sb.append('.');
            sb.append(string);
        }
        return sb.toString();
    }

    /**
     * {@inheritDoc}
     */
    public int compareTo(Version o) {
        int cmp = this.major.compareTo(o.major);
        if (cmp != 0) {
            return cmp;
        }
        cmp = this.minor.compareTo(o.minor);
        if (cmp != 0) {
            return cmp;
        }
        return this.revision.compareTo(o.revision);
    }

    /**
     * @return boolean se esta revisao eh um snapshot
     */
    public boolean isSnapshot() {
        return this.snapshot;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return this.major + "." + this.minor + "." + this.revision + " (" + this.scmRevision + ")";
    }

    /**
     * Verifica se esta versao eh pelo menos a passada (inclusive)
     * 
     * @param version String
     * @return boolean
     */
    public boolean atLeast(String version) {
        return this.compareTo(new Version(version, SCMRevision.NA)) >= 0;
    }

    /**
     * Verifica se esta versao eh no maximo a passada (inclusive)
     * 
     * @param version String
     * @return boolean
     */
    public boolean atMost(String version) {
        return this.compareTo(new Version(version, SCMRevision.NA)) <= 0;
    }

    /**
     * @return a major
     */
    public VersionComponent major() {
        return this.major;
    }

    /**
     * @return a minor
     */
    public VersionComponent minor() {
        return this.minor;
    }

    /**
     * @return a revision
     */
    public VersionComponent revision() {
        return this.revision;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Object#hashCode()
     */

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.major == null) ? 0 : this.major.hashCode());
        result = prime * result + ((this.minor == null) ? 0 : this.minor.hashCode());
        result = prime * result + ((this.revision == null) ? 0 : this.revision.hashCode());
        result = prime * result + ((this.scmRevision == null) ? 0 : this.scmRevision.hashCode());
        final int i = 1231;
        final int j = 1237;
        result = prime * result + (this.snapshot ? i : j);
        return result;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        Version other = (Version) obj;
        if (this.major == null) {
            if (other.major != null) {
                return false;
            }
        } else if (!this.major.equals(other.major)) {
            return false;
        }
        if (this.minor == null) {
            if (other.minor != null) {
                return false;
            }
        } else if (!this.minor.equals(other.minor)) {
            return false;
        }
        if (this.revision == null) {
            if (other.revision != null) {
                return false;
            }
        } else if (!this.revision.equals(other.revision)) {
            return false;
        }
        if (this.scmRevision == null) {
            if (other.scmRevision != null) {
                return false;
            }
        } else if (!this.scmRevision.equals(other.scmRevision)) {
            return false;
        }
        if (this.snapshot != other.snapshot) {
            return false;
        }
        return true;
    }

}
